﻿using Abp.Dependency;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Castle.Core.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace Sunlit.Business.Net.Sms
{
    public class AliyunSmsSender: ISmsSender, ITransientDependency
    {
        public ILogger Logger { get; set; }
        private readonly AliyunSmsSenderConfiguration _aliyunSmsSenderConfiguration;

        public AliyunSmsSender(AliyunSmsSenderConfiguration aliyunSmsSenderConfiguration)
        {
            _aliyunSmsSenderConfiguration = aliyunSmsSenderConfiguration;

            Logger = NullLogger.Instance;
        }
        public void SendSms(SmsMessage message)
        {
            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", _aliyunSmsSenderConfiguration.AppKey, _aliyunSmsSenderConfiguration.Secret);
            DefaultAcsClient client = new DefaultAcsClient(profile);
            CommonRequest request = new CommonRequest();
            request.Method = MethodType.POST;
            request.Domain = "dysmsapi.aliyuncs.com";
            request.Version = "2017-05-25";
            request.Action = "SendSms";
            // request.Protocol = ProtocolType.HTTP;
            request.AddQueryParameters("PhoneNumbers", message.PhoneNumbers);
            request.AddQueryParameters("SignName", string.IsNullOrEmpty(message.SignName) ? _aliyunSmsSenderConfiguration.SignName : message.SignName);
            request.AddQueryParameters("TemplateCode", message.TemplateCode);
            request.AddQueryParameters("TemplateParam", JsonConvert.SerializeObject(message.TemplateParam));
            try
            {
                CommonResponse response = client.GetCommonResponse(request);
                Logger.Info(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
            }
            catch (ServerException e)
            {
                Logger.Error($"Aliyun Sms ErrorCode:{e.ErrorCode},ErrorMessage:{e.ErrorMessage}");
            }
            catch (ClientException e)
            {
                Logger.Error($"Aliyun Sms ErrorCode:{e.ErrorCode},ErrorMessage:{e.ErrorMessage}");
            }
        }
        /// <summary>
        /// 发送注册验证码
        /// </summary>
        /// <param name="phoneNumber">手机号</param>
        /// <param name="code">验证码</param>
        public void SendRegisterCode(string phoneNumber,string code)
        {
            SmsMessage message = new SmsMessage
            {
                PhoneNumbers = phoneNumber,
                TemplateCode = _aliyunSmsSenderConfiguration.RegisterTemplateCode,
                TemplateParam = new Dictionary<string, string> { { "code",code} }
            };
            SendSms(message);
        }
        /// <summary>
        /// 发送重置密码验证码
        /// </summary>
        /// <param name="phoneNumber">手机号</param>
        /// <param name="code">验证码</param>
        public void SendResetPasswordCode(string phoneNumber, string code)
        {
            SmsMessage message = new SmsMessage
            {
                PhoneNumbers = phoneNumber,
                TemplateCode = _aliyunSmsSenderConfiguration.ResetPasswordTemplateCode,
                TemplateParam = new Dictionary<string, string> { { "code", code } }
            };
            SendSms(message);
        }
        /// <summary>
        /// 发送登录验证码
        /// </summary>
        /// <param name="phoneNumber">手机号</param>
        /// <param name="code">验证码</param>
        public void SendLoginCode(string phoneNumber, string code)
        {
            SmsMessage message = new SmsMessage
            {
                PhoneNumbers = phoneNumber,
                TemplateCode = _aliyunSmsSenderConfiguration.LoginTemplateCode,
                TemplateParam = new Dictionary<string, string> { { "code", code } }
            };
            SendSms(message);
        }
    }
}
